package com.lufei.dh.bottom.Fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lufei.dh.R;
import com.lufei.dh.bottom.Bean.Discount;
import com.lufei.dh.bottom.Bean.DiscountViewBinder;
import com.lufei.dh.bottom.Bean.DiscountViewBinder2;
import com.lufei.dh.bottom.Bean.Horizontal_Item;
import com.lufei.dh.bottom.Bean.Horizontal_ItemViewBinder;
import com.lufei.dh.bottom.MultAuto.Item_Auto;
import com.lufei.dh.bottom.MultAuto.Item_AutoViewBinder;
import com.lufei.dh.view.Mult.Adapter.Lam.CategoryViewBinder2;
import com.lufei.dh.view.Mult.Adapter.Lam.SongViewBinder2;
import com.lufei.dh.view.Mult.Adapter.Lam.TwoViewBinder2;
import com.lufei.dh.view.Mult.Bean.Song;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.drakeet.multitype.ClassLinker;
import me.drakeet.multitype.ItemViewBinder;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * 版权：易金卡沃 版权所有
 * <p>
 * 作者：lufei
 * <p>
 * 创建日期：2018/6/25
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
public class Fragment_Mine extends Fragment {

    private MultiTypeAdapter adapter;
    private Items items;
    @BindView(R.id.f3_recyclerview)
    RecyclerView f3Recyclerview;
    Unbinder unbinder;
    private final static int SPAN_COUNT = 7;
    String xc = "http://img2.imgtn.bdimg.com/it/u=390562100,1427089938&fm=27&gp=0.jpg";
    String xc2 = "http://img4.imgtn.bdimg.com/it/u=2682681911,2214217737&fm=27&gp=0.jpg";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment3, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        final GridLayoutManager layoutManager = new GridLayoutManager(getContext(), SPAN_COUNT);
        /* 关键内容：通过 setSpanSizeLookup 来告诉布局，你的 item 占几个横向单位，
         * 如果你横向有 5 个单位，而你返回当前 item 占用 5 个单位，那么它就会看起来单独占用一行 */
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
//                return (items.get(position) instanceof Category) ? SPAN_COUNT : 1;
                return (items.get(position) instanceof Horizontal_Item) ? 1 : SPAN_COUNT;
//                return getManager(position);
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());


        f3Recyclerview.setLayoutManager(linearLayoutManager);

        adapter = new MultiTypeAdapter();
        adapter.register(Item_Auto.class, new Item_AutoViewBinder(getContext()));
        adapter.register(Horizontal_Item.class, new Horizontal_ItemViewBinder(getContext()));

        adapter.register(Discount.class).to(new DiscountViewBinder(getContext()), new DiscountViewBinder2(getContext())).

                withClassLinker(new ClassLinker<Discount>() {
                    @NonNull
                    @Override
                    public Class<? extends ItemViewBinder<Discount, ?>> index(@NonNull Discount data) {
                        if (data.getType() == 0) {
                            return DiscountViewBinder.class;
                        } else if (data.getType() == 1) {
                            return DiscountViewBinder2.class;
                        } else {
                            return DiscountViewBinder2.class;
                        }
                    }
                });
        items = new Items();
        int size = 5;
        int size2 = 5;
        for (int i = 0; i < size; i++) {
            items.add(new Item_Auto("银行" + i, 1));

        }
        items.add(new Horizontal_Item("周", SPAN_COUNT));

        items.add(new Discount("活动", xc, 0));
        items.add(new Discount("活动", xc2, 1));
        items.add(new Discount("活动", xc2, 1));
        items.add(new Discount("活动", xc2, 1));
        items.add(new Discount("活动", xc, 0));
        items.add(new Discount("活动", xc, 0));


        adapter.setItems(items);
//        adapter.notifyDataSetChanged();
//        for (int i = 0; i < items.size(); i++) {
//            f3Recyclerview.setLayoutManager( getManager(i));
//        }
        f3Recyclerview.setAdapter(adapter);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void zxc() {
        int size = 5;
        for (int i = 0; i < size; i++) {
            items.set(i,new Item_Auto("信用卡" + i, 1));
        }
        adapter.notifyDataSetChanged();
    }

}
